import { createStore } from 'vuex'

export default createStore({
  state: {
    count: 0,
    key: "",
    perm_code_dir: {
      name2code: {
        '民乐团': 0x0001, '交响乐团': 0x0002, '管乐团': 0x0004, '行进管乐团': 0x0008, '舞蹈团': 0x0010,
        '合唱团': 0x0020, '话剧团': 0x0040, '主持团': 0x0080, '曲艺团': 0x0100, '踢踏舞团': 0x0200, '罗阳剧团': 0x0400
      },
      code2name: {
        0x0001: '民乐团', 0x0002: '交响乐团', 0x0004: '管乐团', 0x0008: '行进管乐团', 0x0010: '舞蹈团',
        0x0020: '合唱团', 0x0040: '话剧团', 0x0080: '主持团', 0x0100: '曲艺团', 0x0200: '踢踏舞团', 0x0400: '罗阳剧团',
      },
    },
    object_code_dir: {
      object_code2type: {
        0: '排练', 1: '场地', 2: '人员', 3: '乐器', 4: '服装', 5: '奖项', 6: '缺勤', 7: '日志'
      },
      type2object_code: {
        '排练': 0,'场地': 1,'人员': 2,'乐器': 3,'服装': 4, '奖项': 5, '缺勤': 6, '日志': 7
      },
    },

    perm_code: 0,
    post_site_dir: {
      login: "http://localhost:8090/login",
      query_group_information: 'http://localhost:8090/query_group_information',
      query: "http://localhost:8090/query",
      add_data: "http://localhost:8090/add_data",
      edit_data: "http://localhost:8090/edit_data",
      remove_data: "http://localhost:8090/remove_data",
      apply_absent_link: "http://localhost:8090/apply_absent_link",
      check_absent_link: 'http://localhost:8090/check_absent_link',
      add_absent_application: 'http://localhost:8090/add_absent_application',
      register: 'http://localhost:8090/register',
      logout: 'http://localhost:8090/logout',

      rehearse_statistics: 'http://localhost:8090/absent_statistics',
      operation_log: 'http://localhost:8090/operation_log',
    }

  },
  methods: {

  },
  mutations: {
    increment(state) {
      state.count++;
    },
    set_perm_code(state, perm_code) {
      state.perm_code = perm_code
    },
    set_key(state, key) {
      state.key = key
    },
    set_cookies(state) {
      let exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * 7); //保存7天
      //字符串拼接cookie
      if (state.key === '' || state.perm_code === '') {
        alert("this.$store.commit('set_cookies') error: key or perm_code is ''")
        return
      }
      window.document.cookie = "key" + "=" + state.key + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie = "perm_code" + "=" + state.perm_code + ";path=/;expires=" + exdate.toGMTString();
    },
    get_cookies(state) {
      if (document.cookie.length > 0) {
        let arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
        for (let i = 0; i < arr.length; i++) {
          let arr2 = arr[i].split('='); //再次切割
          //判断查找相对应的值
          if (arr2[0] === 'key') {
            state.key = parseInt(arr2[1]); //保存到保存数据的地方
          } else if (arr2[0] === 'perm_code') {
            state.perm_code = parseInt(arr2[1]);
          }
        }
      }
    },
    logout(state) {
      state.key = ''
    },
  },
  actions: {
  },
  modules: {
  }
})
